<script lang="tsx" setup>
import { NButton, NSpace, NSwitch } from 'naive-ui';
import { ref, toRefs } from 'vue';
import { useBoolean } from '@sa/hooks';
import { getPageList } from '@/service/api/system/oss';
import { useTable, useTableOperate } from '@/hooks/common/table';
import { useAppStore } from '@/store/modules/app';
import { $t } from '@/locales';
import { useDict } from '@/utils/dict';
// import UserSearch from './components/user-search.vue';
import DictTag from '@/components/custom/dict-tag.vue';
import type { OssVO } from '@/service/types/system';
import FileSearch from './components/file-search.vue';
import FilePreview from './components/file-preview.vue';
const { sys_normal_disable, sys_grant_type, sys_device_type } = toRefs<any>(
  useDict('sys_normal_disable', 'sys_grant_type', 'sys_device_type')
);
const { bool: visible, setTrue: openModal } = useBoolean();
const appStore = useAppStore();
const currentFileInfo = ref<OssVO>();
const handlePreview = (row: OssVO) => {
  currentFileInfo.value = row;
  openModal();
};
const { columns, columnChecks, data, getData, loading, mobilePagination, searchParams, resetSearchParams } = useTable({
  apiFn: getPageList,
  showTotal: true,
  apiParams: {
    pageNum: 1,
    pageSize: 10,
    fileName: null
  },
  columns: () => [
    {
      type: 'selection',
      align: 'center',
      width: 48
    },
    {
      key: 'fileName',
      title: '文件名',
      align: 'center',
      minWidth: 100,
      ellipsis: {
        tooltip: true
      }
    },
    {
      key: 'originalName',
      title: '原名',
      align: 'center',
      minWidth: 100,
      ellipsis: {
        tooltip: true
      }
    },
    {
      key: 'fileSuffix',
      title: '后缀',
      align: 'center',
      minWidth: 100
    },
    {
      key: 'createByName',
      title: '上传人',
      align: 'center',
      minWidth: 100
    },
    {
      key: 'service',
      title: '服务商',
      align: 'center',
      minWidth: 100
    },
    {
      key: 'createTime',
      title: '上传时间',
      align: 'center',
      minWidth: 100
    },

    {
      key: 'operate',
      title: $t('common.operate'),
      align: 'center',
      fixed: 'right',
      width: 130,
      render: row => (
        <div class="flex-center gap-8px">
          <NButton type="primary" ghost size="small" onClick={() => handlePreview(row)}>
            预览
          </NButton>
          <NButton type="error" ghost size="small" onClick={() => {}}>
            删除
          </NButton>
        </div>
      )
    }
  ]
});
const {
  drawerVisible,
  operateType,
  editingData,
  handleAdd,
  handleEdit,
  checkedRowKeys,
  onBatchDeleted,
  onDeleted
  // closeDrawer
} = useTableOperate(data, getData);
</script>

<template>
  <div class="min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <FileSearch v-model:model="searchParams" @reset="resetSearchParams" @search="getData" />
    <NCard title="文件管理" :bordered="false" size="small" class="sm:flex-1-hidden card-wrapper">
      <template #header-extra>
        <TableHeaderOperation
          v-model:columns="columnChecks"
          :disabled-delete="checkedRowKeys.length === 0"
          :loading="loading"
          @add="handleAdd"
          @refresh="getData"
        />
      </template>
      <NDataTable
        v-model:checked-row-keys="checkedRowKeys"
        :columns="columns"
        :data="data"
        size="small"
        :flex-height="!appStore.isMobile"
        :scroll-x="962"
        :loading="loading"
        remote
        :row-key="row => row.ossId"
        :pagination="mobilePagination"
        class="sm:h-full"
      >
        <template #empty>
          <Empty />
        </template>
      </NDataTable>
    </NCard>
    <FilePreview v-model:visible="visible" :row-data="currentFileInfo" />
  </div>
</template>

<style scoped></style>
